package com.yunxi.service.localService.sys.impl;

import com.yunxi.core.service.ServiceImplPlus;
import com.yunxi.core.web.TreeConfig;
import com.yunxi.dao.sys.RoleUserMapper;
import com.yunxi.model.sys.RoleMenu;
import com.yunxi.model.sys.RoleUser;
import com.yunxi.service.localService.sys.RoleUserService;
import com.yunxi.sys.pojo.*;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * Created by xuruzi on 2017/5/3.
 */
@Service
public class RoleUserServiceImpl extends ServiceImplPlus<RoleUserMapper, RoleUser> implements RoleUserService {

    @Override
    public List listByRoleId(String userId) {
        return baseMapper.listByRoleId(userId);
    }

    @Override
    public List listByRoleCode(String roleCode) {
        return baseMapper.listByRoleCode(roleCode);
    }

    public void saveRoleUserList(RoleUserVO roleMenuExt, String sysUserId) {
        //删除角色下的菜单
        baseMapper.deleteByRoleId(roleMenuExt.getRoleId());
        List<RoleUserVO> ids = roleMenuExt.getRoleUserList();
        //创建角色菜单
        if(ids!=null&&ids.size()>0){
            for(int i = 0 ; i < ids.size() ; i++){
                RoleUserVO ext = ids.get(i);
                RoleUser roleMenu = new RoleUser();
                roleMenu.setRoleId(roleMenuExt.getRoleId());
                roleMenu.setUserId(ext.getUserId());
                baseMapper.insert(roleMenu);
            }
        }
    }

    @Override
    public void delById(String userId) {
        baseMapper.deleteByUserId(userId);
    }

    @Override
    public List<RoleVO> listByDomainId(String domainId) {
        return baseMapper.listByDomainId(domainId);
    }

    @Override
    public List<RoleUserVO> listByUserId(String userId) {
        return baseMapper.listByUserId(userId);
    }

    @Override
    public void saveRoleUserList(RoleUserVO roleUser) {
        List<RoleUserVO> ids = roleUser.getRoleUserList();
        //创建角色菜单
        if(ids!=null&&ids.size()>0){
            //删除角色下的菜单
            baseMapper.deleteByRoleId(roleUser.getRoleId());
            for(int i = 0 ; i < ids.size() ; i++){
                RoleUserVO ext = ids.get(i);
                RoleUser roleMenu = new RoleUser();
                roleMenu.setRoleId(roleUser.getRoleId());
                roleMenu.setUserId(ext.getUserId());
                baseMapper.insert(roleMenu);
            }
        }
    }

    @Override
    public List<RoleVO> listRoleByAccountNo(String accountNo) {
        return baseMapper.listRoleByAccountNo(accountNo);
    }

    @Override
    public List<UserVO> userListByRoleId(String roleId) {
        return baseMapper.userListByRoleId(roleId);
    }
}
